<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      default-expand-all
      show-checkbox
      check-strictly
      :data="list"
      :props="{label:'name'}"
    />
    <el-row type="flex" justify="end">
      <el-button
        type="primary"
        size="mini"
      >取消</el-button>
      <el-button
        type="
        primary"
        size="mini"
        @click="hSubmit"
      >确认</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { transDtaToTreeData } from '@/utils/index'
import { getRolesDetail, getRolesAssignPrem } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await getRolesAssignPrem({ id: this.curId, permIds: permIds })
      this.$emit('success')
    },
    async  loadPermission() {
      try {
        const res = await getPermissionList()
        const detail = await getRolesDetail(this.curId)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        console.log(res, 'getPermissionList')
        this.list = transDtaToTreeData(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

